package com.czy.util.json;

import com.czy.convert.Converter;
import com.czy.util.time.TimeFormat;
import com.czy.util.time.TimeUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.util.Date;

/**
 * jason转对象时，日期字符串转date对象。
 *
 * @author chenzy
 * @since 2019.12.25
 */

public class DeserializerStr2Date extends JsonDeserializer<Date> implements Converter<String, Date> {
    @Override
    public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        return convert(p.getText());
    }

    @Override
    public Date convert(String source) {
        return TimeUtil.str2Time(source, TimeFormat.DEFAULT).map(TimeUtil::toDate).orElse(null);
    }
}
